package com.manager.cms.common;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 分页返回
 *
 */
@Data
public class Pageable<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    //总记录数
    private int totalCount;

    //每页记录数
    private int pageSize;

    //总页数
    private int totalPage;

    //当前页数
    private int currentPage;

    //是否有下一页
    private boolean hasNextPage;

    //列表数据
    private List<T> list;

    /**
     * 分页
     *
     * @param list        列表数据
     * @param totalCount  总记录数
     * @param pageSize    每页记录数
     * @param currentPage 当前页数
     */
    public Pageable(List<T> list, int totalCount, int pageSize, int currentPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNextPage = (currentPage >= totalPage) ? false : true;
    }

    // 简单构造方法
    public Pageable(int pageSize, int currentPage) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    // 通常与简单构造方法一起使用
    public void complete(List<T> list, int totalCount) {
        this.list = list;
        this.totalCount = totalCount;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNextPage = (currentPage >= totalPage) ? false : true;
    }
}
